package org.example;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.List;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-persistence-unit");
        EntityManager em = emf.createEntityManager();
        nativeQuery(em,"SHOW TABLES");
        nativeQuery(em,"SHOW COLUMNS FROM EMPLOYEE_PHONENUMBERS");
        persistEmployees(em);
        nativeQuery(em,"SELECT * FROM EMPLOYEE");
        nativeQuery(em,"SELECT * FROM EMPLOYEE_PHONENUMBERS");
        em.close();
    }
    private static void persistEmployees(EntityManager em){
        Employee e1 = Employee.create("Diana",2000,"IT","111-111-111","222-222-222");
        Employee e2 = Employee.create("Denise",2500,"Admin","333-333-333","444-444-444","666-666-666");
        Employee e3 = Employee.create("Linda",4500,"Sales","555-555-555");
        em.getTransaction().begin();
        em.persist(e1);
        em.persist(e2);
        em.persist(e3);
        em.getTransaction().commit();
    }
    private static void nativeQuery(EntityManager em,String s){
        System.err.println("------------" + s + "---------------");
        Query query = em.createNativeQuery(s);
        List list = query.getResultList();
        for (Object o : list) {
            if (o instanceof Object[]) {
                System.err.println(Arrays.toString((Object[])o));
            }else{
                System.err.println(o);
            }
        }
    }
}
